﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HocSinhManager
{
    public partial class Form1 : Form
    {
        HocSinhSoapService.Service1SoapClient hs = new HocSinhSoapService.Service1SoapClient();
        public Form1()
        {
            InitializeComponent();
            
        }
      

        private void btn_them_Click(object sender, EventArgs e)
        {
            if (kiemtraNhapvao() == 1)
            {
                if (kiemtraMSHH(txt_mshs.Text) == 1)
                {
                    int i = hs.ThemHocSinh(txt_mshs.Text, txt_hoten.Text, datetime_ngaysinh.Value, float.Parse(txt_dtb.Text));
                    if (i == 1)
                        MessageBox.Show("Đã thêm học sinh ");
                    else
                    { }
                }
                else
                    MessageBox.Show("MSHS đã tồn tại");
            }
            else
                MessageBox.Show("Nhập vào không hợp lệ");
            capnhat();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            capnhat();
        }
        public void capnhat()
        {
            try
            {
                datagv.AutoGenerateColumns = false;
                datagv.DataSource = hs.LayDanhSach().Tables[0];
                if (datagv.Rows.Count > 7)
                {
                    datagv.Height = datagv.Rows[0].Height * 7 + 25;
                }
                else
                    datagv.Height = datagv.Rows[0].Height * datagv.Rows.Count + 25;
            }
            catch
            {
                MessageBox.Show("Dữ liệu không hơp lệ !");
            }
           
        }
        public int kiemtraInt(string txt)
        {
            try 
            {
                Int32.Parse(txt);
                return 1; 
            }
            catch
            { 
                return 0;
            }
        }
        public int kiemtraNhapvao()
        {
            if (kiemtraInt(txt_mshs.Text) == 1 && kiemtraFloat(txt_dtb.Text) == 1)
                return 1;
            return 0;
        }
        public int kiemtraFloat(string txt)
        {
            try
            {
                float.Parse(txt);
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        public int kiemtraMSHH(string id)
        {
            string[] mshs = new string[datagv.Rows.Count-1];
            for (int i = 0; i < datagv.Rows.Count-1; i++)
            {
                mshs[i] = datagv.Rows[i].Cells[1].Value.ToString();
            }
            foreach (string ms in mshs)
            {
                if (id == ms)
                    return 0;
            }

                return 1;
        }

        public int kiemtraIntMSHH()
        {
            string[] mshs = new string[datagv.Rows.Count - 1];
            for (int i = 0; i < datagv.Rows.Count - 1; i++)
            {
                mshs[i] = datagv.Rows[i].Cells[1].Value.ToString();
            }
            foreach (string ms in mshs)
            {                
               
                    if (kiemtraInt(ms) == 0)
                        return 0;
            }

            return 1;
        }
        private void datagv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            datagv.Rows[e.RowIndex].Cells["col_stt"].Value = e.RowIndex + 1;
        }

        private void btn_capnhat_Click(object sender, EventArgs e)
        {
            capnhat();
        }
        public int kiemtraDTB()
        {
            
                string[] mshs = new string[datagv.Rows.Count - 1];
                for (int i = 0; i < datagv.Rows.Count - 1; i++)
                {
                    mshs[i] = datagv.Rows[i].Cells[4].Value.ToString();
                }
                foreach (string ms in mshs)
                {
                    if (kiemtraFloat(ms) == 0)
                        return 0;
                }

                return 1;
          
           
        }
        public int kiemtraDateTime(string txt)
        {
            try
            {
                DateTime.Parse(txt);
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        public int kiemtraNgaySinh()
        {
            string[] mshs = new string[datagv.Rows.Count - 1];
            for (int i = 0; i < datagv.Rows.Count - 1; i++)
            {
                mshs[i] = datagv.Rows[i].Cells[3].Value.ToString();
            }
            foreach (string ms in mshs)
            {
                if (kiemtraDateTime(ms)==0)
                    return 0;
            }
            return 1;

            
        }
        public int kiemtraUpdate()
        {
            if (kiemtraIntMSHH() == 1 && kiemtraNgaySinh() == 1 && kiemtraDTB() == 1)
                return 1;
            else
                 return 0;
        }
        private void btn_luu_Click(object sender, EventArgs e)
        {
            if (kiemtraUpdate() == 1 )
            {
                int i = hs.CapNhat((DataTable)datagv.DataSource);
                if (i == 1)
                {
                    MessageBox.Show("Đã lưu thay đổi");
                }
                else
                {
                    MessageBox.Show("MSHS đã tồn tại");
                }
            }
            else
            {
                
                MessageBox.Show("Dữ liệu chỉnh sửa không phù hợp");
            }
            capnhat();
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            if (datagv.SelectedRows.Count > 0)
            {
                try
                {
                    for (int i = 0; i < datagv.SelectedRows.Count; i++)
                    {
                        DataGridViewRow r = datagv.SelectedRows[i];
                        datagv.Rows.Remove(r);
                        int j = hs.CapNhat((DataTable)datagv.DataSource);
                    }
                }
                catch
                {MessageBox.Show("Không thể thực hiện"); }
            }
            else
            {
                try
                {
                    // MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
                    DataGridViewRow r = datagv.CurrentRow;
                    datagv.Rows.Remove(r);
                    int j = hs.CapNhat((DataTable)datagv.DataSource);
                }
                catch { MessageBox.Show("Không thể thực hiện"); }
            }
            capnhat();
        }

       
    }
}
